package com.example.moneybook.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.moneybook.dto.RepaymentDto;
import com.example.moneybook.pojo.Account;
import com.example.moneybook.pojo.BorrowDto;
import com.example.moneybook.dto.ResponseResult;
import com.example.moneybook.enums.AppHttpCodeEnum;
import com.example.moneybook.mapper.BorrowMapper;
import com.example.moneybook.pojo.Borrow;
import com.example.moneybook.service.AccountService;
import com.example.moneybook.service.BorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ObjectOutput;
import java.util.HashMap;
import java.util.List;

/**
 * @Author: 包
 * @Date: 2023/3/1 22:52
 * Description:
 */
@Service
public class BorrowServiceImpl extends ServiceImpl<BorrowMapper, Borrow> implements BorrowService {

    @Autowired
    private BorrowMapper borrowMapper;

    /**
     * 获取某个账户的借款记录
     * @param userId
     * @param accountId
     * @return
     */
    @Override
    public List<Borrow> borrowByAccount(String userId, int accountId) {
        QueryWrapper<Borrow> borrowQueryWrapper = new QueryWrapper<>();
        borrowQueryWrapper
                .eq("user_id", userId)
                .eq("account_id", accountId);
        List<Borrow> borrows = borrowMapper.selectList(borrowQueryWrapper);
        return borrows;
    }


    @Autowired
    private AccountService accountService;
    /**
     * 添加借款记录
     * @param borrow
     * @param accountName
     * @return
     */
    @Override
    public ResponseResult addBorrow(Borrow borrow, String accountName) {
        int accountId = accountService.getAccountId(borrow.getUserId(), accountName);
        borrow.setAccountId(accountId);
        borrow.setRefundMoney(0);
        int res = borrowMapper.insert(borrow);
        int update = accountService.updateAccountMoney(accountId, borrow.getState(), borrow.getBorrowMoney());
        if (res == 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }else {
            return ResponseResult.okResult(res,"成功添加一条借款记录");
        }
    }

    /**
     * 获取用户的所有借出/借入记录
     * @param userId
     * @param state
     * @return
     */
    @Override
    public ResponseResult loadBorrowBill(String userId, int state) {
        int borrowMoney = 0;
        QueryWrapper<Borrow> borrowQueryWrapper = new QueryWrapper<>();
        borrowQueryWrapper
                .eq("user_id",userId)
                .eq("state", state);
        List<Borrow> borrows = borrowMapper.selectList(borrowQueryWrapper);

        //借出/借入 总金额
        borrowMoney = borrowMoney+sumBorrowMoney(userId, state);

        HashMap<String, Object> res = new HashMap<>();
        res.put("borrowMoney", borrowMoney);
        res.put("list", borrows);

        return ResponseResult.okResult(res);
    }

    /**
     * 求和借出/借入金额
     * @param userId
     * @param state
     * @return
     */
    @Override
    public int sumBorrowMoney(String userId, int state) {
            BorrowDto borrowDto = borrowMapper.sumBorrowMoney(userId, state);
            if (borrowDto != null){
                int res = borrowDto.getSumMoney()-borrowDto.getRefundMoney();
                return res;
            }else {
                return 0;
            }

    }

    /**
     * 借入/借出 还款
     * @param repaymentDto
     * @return
     */
    @Override
    public ResponseResult repayment(RepaymentDto repaymentDto) {
        if (!repaymentDto.getAccountName().equals(null)){
            int accountId = accountService.getAccountId(repaymentDto.getUserId(), repaymentDto.getAccountName());
            if (repaymentDto.getBorrowState() == 1){
                accountService.updateAccountMoney(accountId, 0,repaymentDto.getRefundMoney());
            }else if (repaymentDto.getBorrowState() == 0){
                Account accountInfo = accountService.getAccountInfo(repaymentDto.getUserId(), accountId);
                if (accountInfo.getAccountState() == 1){
                    int accountMoney = accountService.getAccountMoney(repaymentDto.getUserId(), accountId);
                    if (accountMoney + repaymentDto.getRefundMoney() > 0){
                        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "该借贷账户偿还金额过多");
                    }
                }

                accountService.updateAccountMoney(accountId, 1,repaymentDto.getRefundMoney());
            }
        }

        QueryWrapper<Borrow> borrowQueryWrapper = new QueryWrapper<>();
        borrowQueryWrapper
                .eq("borrow_id", repaymentDto.getBorrowId())
                .select("refund_money");
        Borrow borrow = borrowMapper.selectOne(borrowQueryWrapper);
        int sumRefund = repaymentDto.getRefundMoney()+borrow.getRefundMoney();
        UpdateWrapper<Borrow> borrowUpdateWrapper = new UpdateWrapper<>();
        borrowUpdateWrapper
                .eq("borrow_id", repaymentDto.getBorrowId())
                .set("refund_money", sumRefund);
        borrowMapper.update(null, borrowUpdateWrapper);

        return ResponseResult.okResult(200, "操作成功");
    }
}
